package com.phms.controller.open;


import com.phms.model.ResultMap;
import com.phms.pojo.Page;
import com.phms.pojo.User;
import com.phms.service.PageService;
import com.phms.service.UserRoleService;
import com.phms.service.UserService;
import com.phms.utils.MD5;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 登录控制类
 */
@Controller("OpenLogin")
@RequestMapping()
public class LoginController {
    @Autowired
    private ResultMap resultMap;
    @Autowired
    private UserService userService;
    @Autowired
    private PageService pageService;
    @Autowired
    private UserRoleService userRoleService;

    private final Logger logger = LoggerFactory.getLogger(LoginController.class);

    /**
     * 返回 尚未登陆信息
     */
    //get一般用于向服务器请求获取数据，请求参数存放在URL中，并在地址栏可见
    // ，而post是向服务器提交数据，数据放置在容器（HTML HEADER）内且不可见；
    //将一个method定义成RequestMethod.GET时，
    // 可以直接通过地址访问，这非常方便我们在开发的时候调用到我们的接口并进行测试
    @RequestMapping(value = "/notLogin", method = RequestMethod.GET)
    @ResponseBody
    public ResultMap notLogin() {
        logger.warn("尚未登陆！");
        return resultMap.success().message("您尚未登陆！");
    }

    /**
     * 返回 没有权限
     */
    @RequestMapping(value = "/notRole", method = RequestMethod.GET)
    @ResponseBody
    public ResultMap notRole() {
        //获取当前用户信息
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user != null) {//表示没有登录，重定向到登录页面
            logger.info("{}---没有权限！", user.getName());
        }
        return resultMap.success().message("您没有权限！");
    }

    /**
     * 演示页面
     **/
    @RequestMapping(value = "/demo/table", method = RequestMethod.GET)
    public String demoTable() {
        return "table";
    }

    @RequestMapping(value = "/demo/tu", method = RequestMethod.GET)
    public String demoTu() {
        return "tu";
    }

    @RequestMapping(value = "/demo/tu1", method = RequestMethod.GET)
    public String tu1() {
        return "tu1";
    }

    @RequestMapping(value = "/demo/tu2", method = RequestMethod.GET)
    public String tu2() {
        return "tu2";
    }

    @RequestMapping(value = "/demo/tu3", method = RequestMethod.GET)
    public String tu3() {
        return "tu3";
    }
/**演示页面**/
    /**
     * Method name: logout <BR>
     * Description: 退出登录 <BR>
     *
     * @return String<BR>
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
//项目中我们习惯直接使用SecurityUtils.getSubject() 获取当前登录用户的信息
        Subject subject = SecurityUtils.getSubject();
        //强制类型转换，获取你存储的principal
        User user = (User) subject.getPrincipal();
        if (null != user) {
            logger.info("{}---退出登录！", user.getName());
        }
        //退出登入
        subject.logout();
        return "login";
    }

    /**
     * Method name: login <BR>
     * Description: 登录验证 <BR>
     * Remark: <BR>
     *
     * @param username 用户名
     * @param password 密码
     * @return ResultMap<BR>
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public ResultMap login(String username, String password) {
        return userService.login(username, password);
    }

    /**
     * Method name: login <BR>
     * Description: 登录页面 <BR>
     *
     * @return String login.html<BR>
     */
    @RequestMapping(value = "/index")
    public String login() {
        return "login";
    }

    /**
     * 注册页面 regist.html
     */
    @RequestMapping(value = "/regist")
    public String regist() {
        return "regist";
    }

    /**
     * 注册
     */
    @RequestMapping(value = "/doRegist")
    @ResponseBody
    public ResultMap doRegist(User user) {
        System.out.println(user);
        User u = userService.getUserByPhoneAndName(user.getPhone(), null);
        if (u != null) {
            return resultMap.success().message("该手机号已注册!");
        }
        try {
            //对获取的密码加密
            user.setPassword(MD5.md5(user.getPassword()));
            user.setCreateTime(new Date());
            userService.save(user);
            String[] ids = new String[1];
            ids[0] = user.getId() + "";
            // 普通用户
            userRoleService.addUserRole(2, ids);
            return resultMap.success().message("注册成功");
        } catch (Exception e) {
            e.printStackTrace();
            return resultMap.fail().message("注册失败");
        }
    }

    /**
     * Method name: index <BR>
     * Description: 登录页面 <BR>
     * @return String login.html<BR>
     */
    @RequestMapping(value = "/")
    public String index(Model model) {
       //获取当前登录用户的信息
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (null != user) {
            //model.addattribute(K,V)往前台传数据，可以传对象
            model.addAttribute("user", user);

            List<Page> pageList = pageService.getAllRolePageByUserId(user.getId() + "");

            model.addAttribute("pageList", pageList);
            return "index";
        } else {
            return "login";
        }
    }

    /**
     * 进入主页面
     * <p>
     * index.html
     */
    @RequestMapping(value = "/main")
    public String main(Model model) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (null != user) {
            model.addAttribute("user", user);
        } else {
            return "login";
        }
        List<Page> pageList = pageService.getAllRolePageByUserId(user.getId() + "");
        model.addAttribute("pageList", pageList);
        return "index";
    }

    /**
     * 检测旧密码是否正确
     * @param password 旧密码
     * @return boolean 是否正确<BR>
     */
    @RequestMapping(value = "/user/checkUserPassword")
    @ResponseBody
    public boolean checkUserPassword(String password) {
        return userService.checkUserPassword(password);
    }

    /**
     * 更新密码
     * @param password 旧密码
     * @return String 是否成功<BR>
     */
    @RequestMapping(value = "/user/updatePassword")
    @ResponseBody
    public String updatePassword(String password) {
        return userService.updatePassword(password);
    }
}
